package wang.hex.codetransfer.code;


import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import wang.hex.codetransfer.code.gui.BufferedImageLuminanceSource;

import javax.xml.bind.DatatypeConverter;
import java.awt.image.BufferedImage;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @Author wujie
 * @Class test.code1
 * @Description
 * @Date 2021/1/5 11:21
 */
public class QRCoderGen {
    public final static Base64.Decoder BASE64_DECODER = Base64.getDecoder();
    public final static Base64.Encoder BASE64_ENCODER = Base64.getEncoder();

    public static BufferedImage encode(byte[] content, int w, int h) {
        String result = DatatypeConverter.printHexBinary(content);
        try {
            Hashtable hints = new Hashtable();
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
            //utf8会对base64编码能生成二维码但是解不开
            hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.ISO_8859_1);
            hints.put(EncodeHintType.MARGIN, 1);
            hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
            //复杂模式
            hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
            BitMatrix bitMatrix = new QRCodeWriter().encode(result, BarcodeFormat.QR_CODE, w, h,
                    hints);
            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
                }
            }
            return image;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static BufferedImage encode(String str, int width, int height) {

        return encode(str.getBytes(), width, height);
    }

    public static byte[] decodeBytes(BufferedImage bf) {
        try {
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bf)));
            Hashtable hints = new Hashtable();
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
            //utf8会对base64编码能生成二维码但是解不开
            hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.ISO_8859_1);
            hints.put(EncodeHintType.MARGIN, 1);
            hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
            //复杂模式
            hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
            Result decode = new MultiFormatReader().decode(bitmap,hints);
            return DatatypeConverter.parseHexBinary(decode.getText());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public static String decodeStr(BufferedImage bf) {
        return new String(decodeBytes(bf));
    }

//    public static void main(String[] args) throws Exception {
//        BufferedImage asdhuoiasdbguisabcksnadonca = encode("ASDHUOIASDBGUISABCKSNADONCA");
//        ImageInputStream imageInputStream = ImageIO.createImageInputStream(asdhuoiasdbguisabcksnadonca);
//        imageInputStream.
//  }
}
